package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.UserThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.IWmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author 21236
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 文件上传
     *
     * @param file@return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        //上传文件，将文件传入minIo，将数据存入数据库
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIAL_UPLOAD_FAILED);
        }
        String preFix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString() + preFix;
        String url;
        try {
            url = fileStorageService.uploadHtmlFile("", fileName, file.getInputStream());
        } catch (IOException e) {
            log.error("素材上传失败", e);
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIAL_UPLOAD_FAILED);
        }
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(UserThreadLocalUtil.getUserId());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult iPage(WmMaterialDto dto) {
        dto.checkParam();
        Page<WmMaterial> page = page(new Page<WmMaterial>(dto.getPage(), dto.getSize()),
                new LambdaQueryWrapper<WmMaterial>().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection())
                        .eq(WmMaterial::getUserId, UserThreadLocalUtil.getUserId())
                        .orderByDesc(WmMaterial::getCreatedTime));
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }
}
